package com.ray.udp_send;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import javax.imageio.ImageIO;

public class ReadImage implements Runnable{
	private static final String ImageFormat="bmp";
	private static BlockingQueue<Image> myResource= new LinkedBlockingQueue<Image>();
	public void run(){
		while(true){
			try {
				Thread.sleep(10);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			while(myResource.isEmpty()==false){
				Image myImage= myResource.peek();
//				System.out.println("ReadImage::myImage:"+myImage.getHeight(null));
				myResource.remove();
				BufferedImage image = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
				Graphics2D g = image.createGraphics();
				g.drawImage(myImage, 0, 0, null);

				ByteArrayOutputStream out = new ByteArrayOutputStream();
				try {
					ImageIO.write(image, ImageFormat, out);
					byte[] allArray= out.toByteArray();
//					System.out.println("ReadImage::allArray:"+allArray.length);
				    new mySplit().get_imageQ().add(allArray);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	//		    System.out.println(Arrays.toString(allArray));
			}
		}
	}
	public static Queue<Image> get_myResource(){
		return myResource;
	}
}

//package udp_client;
//
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.IOException;
//import java.util.Arrays;
//import java.util.LinkedList;
//import java.util.Queue;
//
//import javax.imageio.ImageIO;
//
//public class ReadImage implements Runnable{
//        private static final String ImageFormat="bmp";
//        private static Queue<String> myResource= new LinkedList<String>();
//        public void run(){
//                myResource.add("images/Tsukuba1.bmp");
//        myResource.add("images/Tsukuba2.bmp");
//                while(true){
//                        while(myResource.isEmpty()==false){
//                                String ImageName= myResource.peek();
//                                myResource.remove();
//                                BufferedImage image = null;
//                                try {
//                                        image = ImageIO.read(new File(ImageName));
//                                        ByteArrayOutputStream out = new ByteArrayOutputStream();
//                                        ImageIO.write(image, ImageFormat, out);
//                                        byte[] allArray= out.toByteArray();
//                                        System.out.println("allArray size:"+allArray.length);
//                                    new mySplit().get_imageQ().add(allArray);
//                                    Thread.sleep(500);
//                                } catch (IOException e) {
//                                        // TODO Auto-generated catch block
//                                        e.printStackTrace();
//                                }
//        //                  System.out.println(Arrays.toString(allArray));
//                                        catch (InterruptedException e) {
//                                        // TODO Auto-generated catch block
//                                        e.printStackTrace();
//                                }
//                        }
//                }
//        }
//}
